// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   register_draft_ok.java

package fund.gather;

import include.auto_execute.GatherExpiry;
import include.auto_execute.GatherSumLimit;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import purchase.Fieldvalue;
import validata.ValidataNumber;

public class register_draft_ok extends HttpServlet
{

	ServletContext application;
	HttpSession session;

	public register_draft_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			HttpSession session = request.getSession();
			java.io.PrintWriter out = response.getWriter();
			nseer_db_backup1 fund_db = new nseer_db_backup1(dbApplication);
			nseer_db_backup1 crm_db = new nseer_db_backup1(dbApplication);
			nseer_db_backup1 hr_db = new nseer_db_backup1(dbApplication);
			ValidataNumber validata = new ValidataNumber();
			GatherSumLimit gatherSum = new GatherSumLimit();
			GatherExpiry gatherExpiry = new GatherExpiry();
			Fieldvalue fieldValue = new Fieldvalue();
			if (fund_db.conn((String)dbSession.getAttribute("unit_db_name")) && crm_db.conn((String)dbSession.getAttribute("unit_db_name")) && hr_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				Date now = new Date();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String time = formatter.format(now);
				String fund_ID = request.getParameter("fund_ID");
				String account_ID[] = request.getParameterValues("account_ID");
				String subtotal[] = request.getParameterValues("subtotal");
				int p = 0;
				for (int i = 0; i < account_ID.length; i++)
					if (!subtotal[i].equals(""))
					{
						String subtotal1 = "";
						for (StringTokenizer tokenTO = new StringTokenizer(subtotal[i], ","); tokenTO.hasMoreTokens();)
							subtotal1 = (new StringBuilder(String.valueOf(subtotal1))).append(tokenTO.nextToken()).toString();

						if (!validata.validata(subtotal1))
							p++;
					}

				if (p == 0)
				{
					String register = request.getParameter("register");
					String register_time = request.getParameter("register_time");
					String currency_name = request.getParameter("currency_name");
					String personal_unit = request.getParameter("personal_unit");
					String fund_chain_ID[] = request.getParameterValues("fund_chain_ID");
					String fund_chain_name[] = request.getParameterValues("fund_chain_name");
					String account_bank[] = request.getParameterValues("account_bank");
					String cost_price_subtotal[] = request.getParameterValues("cost_price_subtotal");
					String executed_cost_price_subtotal[] = request.getParameterValues("executed_cost_price_subtotal");
					String execute_method[] = request.getParameterValues("execute_method");
					String bill_ID[] = request.getParameterValues("bill_ID");
					String details_number[] = request.getParameterValues("details_number");
					int n = 0;
					for (int i = 0; i < account_ID.length; i++)
						if (!subtotal[i].equals(""))
						{
							String subtotal1 = "";
							for (StringTokenizer tokenTO = new StringTokenizer(subtotal[i], ","); tokenTO.hasMoreTokens();)
								subtotal1 = (new StringBuilder(String.valueOf(subtotal1))).append(tokenTO.nextToken()).toString();

							String sql1 = (new StringBuilder("select * from fund_executing where fund_ID='")).append(fund_ID).append("' and account_bank='").append(account_bank[i]).append("' and account_ID='").append(account_ID[i]).append("' and check_tag='0'").toString();
							ResultSet rs1 = fund_db.executeQuery(sql1);
							if (rs1.next())
								n++;
							if (Math.abs(Double.parseDouble(subtotal1) + Double.parseDouble(executed_cost_price_subtotal[i])) > Math.abs(Double.parseDouble(cost_price_subtotal[i])))
								n++;
							if (bill_ID[i].indexOf("'") != -1 || bill_ID[i].indexOf("\"") != -1 || bill_ID[i].indexOf(",") != -1)
								n++;
						}

					if (n == 0)
						try
						{
							int gather_time = 0;
							String sql = (new StringBuilder("select gather_time from fund_fund where fund_ID='")).append(fund_ID).append("'").toString();
							ResultSet rset = fund_db.executeQuery(sql);
							if (rset.next())
								gather_time = rset.getInt("gather_time") + 1;
							double cost_price_sum = 0.0D;
							for (int i = 0; i < account_ID.length; i++)
								if (!subtotal[i].equals(""))
								{
									String subtotaling = "";
									for (StringTokenizer tokenTO = new StringTokenizer(subtotal[i], ","); tokenTO.hasMoreTokens();)
										subtotaling = (new StringBuilder(String.valueOf(subtotaling))).append(tokenTO.nextToken()).toString();

									sql = (new StringBuilder("update fund_details set execute_details_tag='1',execute_check_tag='1' where fund_ID='")).append(fund_ID).append("' and details_number='").append(details_number[i]).append("'").toString();
									fund_db.executeUpdate(sql);
									String sql4 = (new StringBuilder("insert into fund_executing(fund_ID,fund_chain_ID,details_number,fund_chain_name,account_bank,account_ID,subtotal,cost_price_subtotal,register,register_time,executed_cost_price_subtotal,execute_method,bill_ID,currency_name,personal_unit,gather_time) values('")).append(fund_ID).append("','").append(fund_chain_ID[i]).append("','").append(details_number[i]).append("','").append(fund_chain_name[i]).append("','").append(account_bank[i]).append("','").append(account_ID[i]).append("','").append(subtotaling).append("','").append(cost_price_subtotal[i]).append("','").append(register).append("','").append(register_time).append("','").append(executed_cost_price_subtotal[i]).append("','").append(execute_method[i]).append("','").append(bill_ID[i]).append("','").append(currency_name).append("','").append(personal_unit).append("','").append(gather_time).append("')").toString();
									fund_db.executeUpdate(sql4);
								}

							String sql2 = (new StringBuilder("update fund_fund set execute_tag='1',gather_time='")).append(gather_time).append("',check_tag='5' where fund_ID='").append(fund_ID).append("'").toString();
							fund_db.executeUpdate(sql2);
							response.sendRedirect("fund/gather/register_draft_ok.jsp?finished_tag=1");
						}
						catch (Exception ex)
						{
							ex.printStackTrace();
						}
					else
						response.sendRedirect("fund/gather/register_draft_ok.jsp?finished_tag=3");
				} else
				{
					response.sendRedirect("fund/gather/register_draft_ok.jsp?finished_tag=4");
				}
				fund_db.commit();
				hr_db.commit();
				crm_db.commit();
				fund_db.close();
				crm_db.close();
				hr_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
